import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;
import java.util.StringTokenizer;

public class Controller{
	public static void main(String args[]){
		
		int peerInfo[][];
		String peerInfoHostNames[];
		// Initialization Code for reading file
		/*
		 * read System.properties into parameters
		 * start machines
		 */
		//Load the properties file
		try{
			System.out.println("Make sure config.txt is in place");
			Properties prop = new Properties();

			InputStream is = new FileInputStream("C://Users//Sirisha//workspace//BitTorrentCN//src//common.cfg");
			prop.load(is);
			//Read the properties file parameters...
			int numberOfPreferredNeighbors = Integer.parseInt(prop.getProperty("NumberOfPreferredNeighbors"));
			int unchokingInterval  = Integer.parseInt(prop.getProperty("UnchokingInterval"));
			int optimisticUnchokingInterval  = Integer.parseInt(prop.getProperty("OptimisticUnchokingInterval"));
			String fileName = prop.getProperty("FileName");
			long fileSize = Integer.parseInt(prop.getProperty("FileSize")); // check if can be parsed to long 
			int pieceSize = Integer.parseInt(prop.getProperty("PieceSize"));
			
			// Read peerdata.cfg
			FileReader fr = new FileReader("C://Users//Sirisha//workspace//BitTorrentCN//src//PeerInfo.cfg");
	        BufferedReader br = new BufferedReader(fr);
		    String read;
		    StringTokenizer readTokenizer;
		    int numberOfPeers = 0;
		    
		    //read number of peers (lines) in file
		    br.mark(9999999);
	        while((read = br.readLine()) != null) {
		    	numberOfPeers++;
		    }
		    //go to the start of file
		    br.reset();
		    peerInfo = new int[numberOfPeers][4];
		    peerInfoHostNames = new String[numberOfPeers];
		    //read the parameters into peerInfo
		    int index = 0;
		    while((read = br.readLine()) != null) {
		    	readTokenizer = new StringTokenizer(read);
		    	peerInfo[index][0] = Integer.parseInt(readTokenizer.nextToken());
		    	peerInfo[index][1] = -1; // indicates read string instead
		    	peerInfoHostNames[index] = readTokenizer.nextToken();
		    	peerInfo[index][2] = Integer.parseInt(readTokenizer.nextToken());
		    	peerInfo[index][3] = Integer.parseInt(readTokenizer.nextToken());
		    	index++;
		    }
		    System.out.println("NO Error reading cfg");
			
			
		} catch (Exception e){
			System.out.println("Error reading config files!");
			e.printStackTrace();
		}
	}
}